package com.nmsl.system.presc_temp.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.nmsl.common.annotation.Log;
import com.nmsl.common.core.controller.BaseController;
import com.nmsl.common.core.domain.AjaxResult;
import com.nmsl.common.enums.BusinessType;
import com.nmsl.system.presc_temp.domain.PrescriptionTemplate;
import com.nmsl.system.presc_temp.service.IPrescriptionTemplateService;
import com.nmsl.common.utils.poi.ExcelUtil;
import com.nmsl.common.core.page.TableDataInfo;

/**
 * 处方模板Controller
 * 
 * @author gmh
 * @date 2025-02-18
 */
@RestController
@RequestMapping("/system/presc_temp")
public class PrescriptionTemplateController extends BaseController
{
    @Autowired
    private IPrescriptionTemplateService prescriptionTemplateService;

    /**
     * 查询处方模板列表
     */
    @PreAuthorize("@ss.hasPermi('system:presc_temp:list')")
    @GetMapping("/list")
    public TableDataInfo list(PrescriptionTemplate prescriptionTemplate)
    {
        startPage();
        List<PrescriptionTemplate> list = prescriptionTemplateService.selectPrescriptionTemplateList(prescriptionTemplate);
        return getDataTable(list);
    }

    /**
     * 导出处方模板列表
     */
    @PreAuthorize("@ss.hasPermi('system:presc_temp:export')")
    @Log(title = "处方模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PrescriptionTemplate prescriptionTemplate)
    {
        List<PrescriptionTemplate> list = prescriptionTemplateService.selectPrescriptionTemplateList(prescriptionTemplate);
        ExcelUtil<PrescriptionTemplate> util = new ExcelUtil<PrescriptionTemplate>(PrescriptionTemplate.class);
        util.exportExcel(response, list, "处方模板数据");
    }

    /**
     * 获取处方模板详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:presc_temp:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(prescriptionTemplateService.selectPrescriptionTemplateById(id));
    }

    /**
     * 新增处方模板
     */
    @PreAuthorize("@ss.hasPermi('system:presc_temp:add')")
    @Log(title = "处方模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PrescriptionTemplate prescriptionTemplate)
    {
        return toAjax(prescriptionTemplateService.insertPrescriptionTemplate(prescriptionTemplate));
    }

    /**
     * 修改处方模板
     */
    @PreAuthorize("@ss.hasPermi('system:presc_temp:edit')")
    @Log(title = "处方模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PrescriptionTemplate prescriptionTemplate)
    {
        return toAjax(prescriptionTemplateService.updatePrescriptionTemplate(prescriptionTemplate));
    }

    /**
     * 删除处方模板
     */
    @PreAuthorize("@ss.hasPermi('system:presc_temp:remove')")
    @Log(title = "处方模板", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(prescriptionTemplateService.deletePrescriptionTemplateByIds(ids));
    }

  //  根据模板id查询处方信息
    @PreAuthorize("@ss.hasPermi('system:presc_temp:query')")
    @GetMapping("/getChufangInfo")
    public AjaxResult getPrescInfo(Long id)
    {
        if (prescriptionTemplateService.selectMedId(id)>0 ){
            return AjaxResult.success(prescriptionTemplateService.selectPrescriptionTemplateById(id));
        }
        if (prescriptionTemplateService.selectExamId(id)>0 ){
            return AjaxResult.success(prescriptionTemplateService.selectExaminationInfoById(id));
        }
        return  AjaxResult.error("查询失败,该模板没有对应的处方");
    }


}
